package com.example.kotlinlearn

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Button

class TestActivity : BaseActivity() {
    @SuppressLint("ResourceAsColor")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        val data = intent.getStringExtra("param1")
        val button:Button = findViewById(R.id.testButton)
        val buttonBack:Button = findViewById(R.id.testButtonBack)
        val buttonExit:Button = findViewById(R.id.testButtonExit)
        button.setOnClickListener {
            if (button.text=="接收数据") button.text=data else button.text = "接收数据"
            button.setBackgroundColor(R.color.purple_200)
        }
        buttonBack.setOnClickListener {
            val intent = Intent()
            intent.putExtra("data","清晨入古寺")
            //设置返回数据和返回码
            setResult(RESULT_OK,intent)
            finish()
        }
        buttonExit.setOnClickListener {
            ActivityCollector.finishAll()
        }
    }

    /**
     * 配置TestActivity启动的方法，如果别人要启动这个活动，调用actionStart方法就行
     * companion object这个修饰相当于静态方法
     */
    companion object {
        fun actionStart(context: Context,data1:String,data2:Int){
            val intent = Intent(context,TestActivity::class.java)
            intent.putExtra("param1",data1)
            intent.putExtra("param2",data2)
            context.startActivity(intent)
        }
    }

    /**
     * 在按下返回按钮时也传递数据
     */
    override fun onBackPressed() {
        val intent = Intent()
        intent.putExtra("data","清晨入古寺")
        //设置返回数据和返回码
        setResult(RESULT_OK,intent)
        super.onBackPressed()
    }
}